﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using GameServer.Controller;
using Common;

namespace GameServer.Servers
{
    class Server
    {
        private IPEndPoint ipEndPoint;
        private Socket serverSocket;
        private ControllerManager controllerManager;

        //缓存客户端
        private List<Client> clientList = new List<Client>();
        private List<Room> roomList = new List<Room>();

        public Server() { }
        public Server(string ipStr, int port)
        {
            controllerManager = new ControllerManager(this);
            SetIPAndPort(ipStr, port);
        }

        /// <summary>
        /// 设置ip和port
        /// </summary>
        /// <param name="ipStr"></param>
        /// <param name="port"></param>
        public void SetIPAndPort(string ipStr, int port)
        {
            ipEndPoint = new IPEndPoint(IPAddress.Parse(ipStr), port);
        }

        /// <summary>
        /// 启动服务器
        /// </summary>
        public void Start()
        {
            //创建服务器
            serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            serverSocket.Bind(ipEndPoint);
            //监听
            serverSocket.Listen(0);
            serverSocket.BeginAccept(AcceptCallBack, null);
        }

        /// <summary>
        /// 接收回调
        /// </summary>
        /// <param name="ar"></param>
        private void AcceptCallBack(IAsyncResult ar)
        {
            Socket clientSocket = serverSocket.EndAccept(ar);
            Client client = new Client(clientSocket, this);
            client.Start();
            clientList.Add(client);
            serverSocket.BeginAccept(AcceptCallBack, null);
        }

        /// <summary>
        /// 移除客户端
        /// </summary>
        /// <param name="_client"></param>
        public void RemoveClient(Client _client)
        {
            lock (clientList)
            {
                clientList.Remove(_client);
            }
        }

        /// <summary>
        /// 给客户端反馈
        /// </summary>
        /// <param name="client"></param>
        /// <param name="requestCode"></param>
        /// <param name="actionCode"></param>
        /// <param name="data"></param>
        public void SendResponse(Client client, ActionCode actionCode, string data)
        {
            client.Send(actionCode, data);
        }

        /// <summary>
        /// 接收客户端数据转给ControllerManager分发给相应的Controller去处理
        /// </summary>
        /// <param name="requestCode"></param>
        /// <param name="actionCode"></param>
        /// <param name="data"></param>
        /// <param name="client"></param>
        public void HandleRequest(RequestCode requestCode, ActionCode actionCode, string data, Client client)
        {
            controllerManager.HandleRequest(requestCode, actionCode, data, client);
        }

        /// <summary>
        /// 创建房间
        /// </summary>
        /// <param name="client">创建者</param>
        public void CreateRoom(Client client)
        {
            Room room = new Room();
            room.AddClient(client);
            roomList.Add(room);
        }

        /// <summary>
        /// 房间列表
        /// </summary>
        public List<Room> GetRooms()
        {
            return roomList;
        }
    }
}
